package com.yisu.expressway.onedollar.activity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import com.yisu.expressway.R;
import com.yisu.expressway.activity.BaseActivity;
import com.yisu.expressway.login.LoginPageActivity;
import com.yisu.expressway.model.LoginEvent;
import com.yisu.expressway.login.LoginManager;

import android.content.Intent;
import android.os.Bundle;

/**
 * Created by Kaven on 2016/12/7.
 */

public abstract class BasicActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(getThemeId());
        int layoutResID = getInflateLayoutId();
        if (layoutResID <= 0) {
            finish();
            return;
        }

        setContentView(layoutResID);
        if (isNeedLogin()) {
            if (!LoginManager.isLoggedIn()) {
                startTargetActivity(LoginPageActivity.class);
                finish();
                return;
            }
        }
        initPresenter();

        Intent intent = getIntent();
        if (intent != null) {
            getIntentData(intent);
        }

        if (savedInstanceState != null) {
            getSavedBundleData(savedInstanceState);
        }

        initViews();
        bindEvents();
        EventBus.getDefault().register(this);
    }

    @Subscribe
    public void onReceiverLoginEvent(LoginEvent event) {
        onLoginStateChange(event.getLoginResult());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        releasePresenter();
    }
    protected abstract int getThemeId();
    /**
     * 获取layout Id
     *
     * @return layout Id
     */
    protected abstract int getInflateLayoutId();

    protected abstract void initPresenter();

    protected abstract void releasePresenter();

    protected abstract void getIntentData(Intent intent);

    protected abstract void getSavedBundleData(Bundle bundle);

    /**
     * 所有初始化View的地方
     */
    protected abstract void initViews();

    /**
     * View绑定事件
     */
    protected abstract void bindEvents();

    protected abstract boolean isNeedLogin();

	protected abstract void onLoginStateChange(boolean login);
}
